import com from "./com";
import config from "./config";
import api from "./api";
/*
<mescroll-body ref="mescrollRef" @init="mescrollInit" :down="downOption"  @down="downCallback" :up="upOption" @up="upCallback"@emptyclick="emptyClick">
	<view v-for="item in data">{{item.sn}}</view>
</mescroll-body>

import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
import List from '../../assets/js/list.js'
let orders = new List()

orders.initMescroll(this).setUrl('/api/streamOrders').setCbFunc(res=>{
	this.data = res
	this.clearLoading()
}).getList()


*/

function List () {
	return {
		page: 1,
		url: "",
		data: {},
		listOption:{
			_where:'',
			_order:'',
			_search:'',
			page:1,
			limit:10,
			_g:''
		},
		list: [],
		noMoreData: false,
		cbFunction: undefined,
		animateFunc: undefined,
		lastRequestTime: 0,
		lastRequestData: undefined,
		lastNotEmptyData: [],
		scrollObject: undefined,
		getList: function(str) {
			console.log(str)
			let _this = this;
			if(_this.url === '')return _this
			if (_this.noMoreData) {
				com.showToast("没有更多数据了")
				_this.cbFunction(_this.list);
				if(_this.scrollObject !== undefined && _this.scrollObject!=null){
					console.log(_this.scrollObject)
					_this.scrollObject.endSuccess(_this.list.length,false)
				}
			} else {
				_this.data.page = this.page
				if (_this.animateFunc !== undefined) {
					_this.animateFunc()
				}
				if(_this.lastRequestData == _this.data && com.getTimestampMs() - _this.lastRequestTime < 1000){
					// return _this.getList;
					return;
				}
				// this.log('aaa')
				_this.lastRequestData = _this.data
				_this.lastRequestTime = com.getTimestampMs()
				api.get(_this.url, _this.data, (res)=> {
							// this.log(_this.scrollObject)
					if(_this.scrollObject !== undefined && _this.scrollObject!=null){
						// this.log(_this.scrollObject)
						_this.scrollObject.endSuccess(res.d.length,res.meta.total >= res.meta.page * res.meta.limit)
					}
					if (_this.lastNotEmptyData === res.d) {
						return _this.getList;
					}
					if (res.d.length === 0 && _this.list.length > 0) {
						com.showToast("没有更多数据了");
						_this.noMoreData = true;
						// _this.cbFunction(_this.list,res.meta);
						_this.cbFunction(_this.list,res);
					} else {
						
						let item = res.d[0]
						//检查数据是否已经存在,如果存在则不继续执行
						if(com.isInArray(item,_this.list)){
							return;
						}
						
						_this.lastNotEmptyData = res.d;
						let orgList = _this.list
						_this.list = orgList.concat(res.d);
						// _this.cbFunction(_this.list,res.meta);
						_this.cbFunction(_this.list,res);
						_this.page++;
					}
				});
		
			}
		},
		reload: function(str) {
			let _this = this;
			if(_this.url === '')return
			_this.page = 1;
			_this.list = [];
			_this.noMoreData = false;
			if(_this.scrollObject !== undefined && _this.scrollObject!=null){
				// _this.scrollObject.resetUpScroll()
			}
			_this.getList();
		
		},
		setPage: function(page) {
			let _this = this;
			_this.page = page
			return _this;
		},
		setUrl: function(url) {
			let _this = this;
			_this.url = url
			return _this;
		},
		setData: function(data) {
			let _this = this;
			_this.data = data
			_this.data.page = this.page
			return _this;
		},
		setCbFunc: function(func) {
			let _this = this;
			_this.cbFunction = func
			return _this;
		},
		setAnimateFunc: function(func) {
			let _this = this;
			_this.animateFunc = func
			return _this;
		},
		//mescroll 专属行为
		VUE:null,
		initMescroll: function(VUE){
			let _this = this;
			_this.scrollObject = VUE.mescroll
			VUE.downCallback = function (){
				_this.reload()
			}
			// VUE.upCallback = function (){
			// 	_this.getList()
			// }
			_this.scrollObject.onReachBottom = function(){
				_this.scrollObject.triggerUpScroll()
				_this.scrollObject.setPageSize(_this.limit)
				_this.getList()
			}
			// _this.scrollObject.onPullDownRefresh = function(){
			// 	// _this.scrollObject.resetUpScroll(true)
			// 	_this.scrollObject.triggerDownScroll()
			// 	_this.reload()
			// }
			return _this
		},
		initMescrollReal: function(VUE){
			let _this = this;
			_this.scrollObject = VUE.mescroll
			VUE.downCallback = function (){
				_this.reload()
			}
			VUE.upCallback = function (){
				_this.getList()
			}
			// VUE.upCallback = _this.getList
			return _this
		},
		
		setOption(option){
			if(option._where != undefined){
				this.listOption._where = option._where
			}
			if(option._order != undefined){
				this.listOption._order = option._order
			}
			if(option._search != undefined){
				this.listOption._search = option._search
			}
			if(option.page != undefined){
				this.listOption.page = option.page
			}
			if(option.limit != undefined){
				this.listOption.limit = option.limit
			}
			if(option.group != undefined){
				this.listOption._g = option.group
			}
			this.setData(this.listOption)
			return this
		},
		setGroup(group){
			if(group){
				this.listOption._g = group
				this.setData(this.listOption)
			}
			return this
		},
		setLimit(limit){
			if(limit){
				this.listOption.limit = limit
				this.setData(this.listOption)
			}
			return this
		},
		setPage(page){
			if(page != undefined){
				this.listOption.page = page
				this.setPage(page)
			}
			return this
		},
		setSearch(_search){
			if(_search != undefined){
				this.listOption._search = _search
				this.setData(this.listOption)
			}
			return this
		},
		setOrder(_order){
			if(_order != undefined){
				this.listOption._order = _order
				this.setData(this.listOption)
			}
			return this
		},
		setWhere(_where){
			if(_where != undefined){
				this.listOption._where = _where
				this.setData(this.listOption)
			}
			return this
		},
		
	}
}

module.exports = List